package com.example.springboot1.limit;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.springboot1.entity.DutyExcelBo;
import com.example.springboot1.tools.CustomMergeStrategy;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ckj
 * @description TODO
 * @date 2023-03-10 9:33
 */


public class Downloader implements Runnable {

    private String id;

    @Override
    public void run() {
        System.out.println("ddddd");
        HttpServletResponse resp = ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
        this.duty(resp);
    }

    public Downloader(String id) {
        this.id = id;
    }


    public void duty(HttpServletResponse response){

        OutputStream out = null;
        BufferedOutputStream bos = null;

        try {
            List<DutyExcelBo> result=new ArrayList<>();
            DutyExcelBo dutyExcelBo=new DutyExcelBo();
            dutyExcelBo.setSort("测试");
            dutyExcelBo.setDutyUserName("jack");
            dutyExcelBo.setDutyDate("10.8");
            DutyExcelBo dutyExcelBo1=new DutyExcelBo();
            dutyExcelBo1.setSort("测试");
            dutyExcelBo1.setDutyUserName("jack");
            dutyExcelBo1.setDutyDate("10.9");
            result.add(dutyExcelBo1);
            InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("templates/record.xlsx");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String name =  "值班记录.xlsx";
            String fileName = URLEncoder.encode(name, "utf-8");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));

            out = response.getOutputStream();
            bos = new BufferedOutputStream(out);

            ExcelWriter excelWriter = EasyExcel.write(bos).withTemplate(resourceAsStream).excelType(ExcelTypeEnum.XLSX).build();

            WriteSheet writeSheet = EasyExcel.writerSheet()
                    .registerWriteHandler(new CustomMergeStrategy(result.stream().map(DutyExcelBo::getSort).collect(Collectors.toList()), 0))
                    .build();
            excelWriter.fill(result, writeSheet);

            Map<String, Object> map = new HashMap<>();

            map.put("title", "2023-11");
            excelWriter.fill(map, writeSheet);
            excelWriter.finish();
            bos.flush();

            excelWriter.finish();
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
